#!/bin/bash
basepwd=/usr/local/shell/alter
filepwd=/usr/local/shell/alter/readfile
cd $filepwd
#stime=`cat stime.log`
mycnf="mysql --defaults-file=/usr/local/shell/.my.cnf"
#告警主题
majorAlarm="MysqlTooManyConnections|MysqlDown|p2pserverClinetOnline|SlbTrafficTXUtilization5M|KafkaTopicHeapUp|HostOutOfDiskSpaceLess(<3%)"
newAlertlog=newAlert.log
alerTime=`date +%s`
#过滤掉部分告警数据
#需求 1.手动启动  2.定时启动      3.
#展示  排序,禁止时间段,不禁止时间段 
#过滤内容, 过滤和多重过滤？ 

filterFunc(){
#根据配置文件进行动态精准过滤
$mycnf -N -e " select concat_ws('￥',filterName,filterHost) from AlarmFilter where state='1';">filter.log


>$newAlertlog
index=0
for filterValue in `cat filter.log`
do
	filterName=`echo $filterValue| awk -F '￥' '{print $1}'`
	filerHost=`echo $filterValue| awk -F '￥' '{print $2}'`
	
	cat source.log | awk -v filterName=$filterName -F '￥' '{ if  ($2 == filterName) print $0}' |  grep -Ev "$filerHost" >>$newAlertlog
	
	let index+=1
    if [[ $index -eq 1 ]];then
       loseName+="$filterName"
	else
       loseName+="|$filterName"
    fi
done
#将其他数据写入新文件中
if [[ $loseName == "" ]];then
    cat source.log  >>$newAlertlog
else
    cat source.log  |  grep -Ev "$loseName" >>$newAlertlog
fi
}

#时间段内禁止或开启
if 


#对比文件将对应数据写入数据库
add_restore(){
#获取目前存在问题的数据,追加到旧文件中
$mycnf -N -e "select concat_ws('￥',alterid,value,fingerprint) from collectAlarm where state='1';" > oldAlerts.log
#判断2个文件是否有差异
#diffnum=`cat $newAlertlog oldAlerts.log  |  grep -Ev 'CPU 负载|当前可用内存'  | awk   -F '￥' '{print $1","$2","$3","$4}'  | awk  -F '[,| ]' '{if ($(NF-2)=="VALUE")  {print  $1","$2","$3} else {print  $0}}' | sort -t ',' -k2 -k3 | uniq -u | wc -l`
diffnum=`cat $newAlertlog oldAlerts.log   | awk   -F '￥' '{print $NF}' |  sort | uniq -u |wc -l`
if [[ $diffnum -eq 0  ]];
then
	echo "告警没变化"
else
	#根据fingerprint,存在与旧文件的为恢复告警,新文件的为新增告警
	cat $newAlertlog oldAlerts.log  | awk   -F '￥' '{print $NF}' |  sort | uniq -u | while read diffLine
	do
	    oldnum=`grep "$diffLine" oldAlerts.log | wc -l`
	    newnum=`grep "$diffLine" $newAlertlog | wc -l`
	    if [[  $oldnum  -ge 1  ]];then
	        changeValue=`grep "$diffLine" oldAlerts.log`
	        alterid=`echo $changeValue | awk -F '￥' '{print $1}'`
	        restoretime=`date +%s`
	        $mycnf -e "update collectAlarm set state='0',restoretime='$restoretime' where alterid='$alterid';"
	        #$mycnf -e "insert into alertime (alertid,recorttime,state) values ('alterid','') where alterid="$alterid";"
	    fi
	    if [  $newnum -ge 1  ];then
   	        changeValue=`grep "$diffLine" $newAlertlog`
   	        activetime=`echo $changeValue | awk -F '￥' '{print $1}'`
   	        activetime=`date -d "$activetime" +%s`
   	        #activetime=`date -d "@$tmptime" '+%F %T'`
		    alertname=`echo $changeValue | awk -F '￥' '{print $2}'`
		    hostname=`echo $changeValue | awk -F '￥' '{print $3}'`
		    severity=`echo $changeValue | awk -F '￥' '{print $4}'`
		    description=`echo $changeValue | awk -F '￥' '{print $5}'`
		    value=`echo $changeValue | awk -F '￥' '{print $6}'`
		    fingerprint=`echo $changeValue | awk -F '￥' '{print $7}'`
	   	    $mycnf -e "insert into collectAlarm (activetime,alertname,hostname,description,severity,value,state,fingerprint) values ('$activetime','$alertname','$hostname','$description','$severity','$value','1','$fingerprint');"
	    fi
	done
fi
}

#如果对比新文件value值有变动,则进行数据更新
update_value(){
	#cat $newAlertlog oldAlerts.log  |  grep -Ev 'CPU 负载|当前可用内存' | awk  -F '￥' '{print $1","$2","$3","$4}' | awk  -F '[,| ]' '{if ($(NF-2)=="VALUE")  {print  $1","$2","$3} else {print  $0}}' | sort -t ',' -k2 -k3 | uniq | while read diffLine
	cat  oldAlerts.log   | awk   -F '￥' '{print $(NF-1)"￥"$NF}'  | while read oldValue
	do
		oldfingerprint=`echo $oldValue | awk -F '￥' '{print $2}'`
	    changeValue=`grep $oldfingerprint $newAlertlog`
	    nulValue=`grep $oldValue $newAlertlog | wc -l`
	    #如果fingerprint存在新文件,但是fingerprint+value不等于新文件,则将value值更新
	    if [[ -n $changeValue ]]  ||  [[ -z  $nulValue  ]];then
			value=`echo $changeValue | awk -F '￥' '{print $6}'`
		    $mycnf -e "update collectAlarm  set value='$value' where fingerprint='$oldfingerprint';"
		else
			echo "无须更新"
		fi
	done
}

#根据排班表,对业务有影响并且很少告警的告警增加短信告警,如果以收到,通过url进行静默告警
#watchList.csv,alterStatus.csv 也可以写入数据库
sms_alter(){
	watchPeople=$(grep `date +%F` watchList.csv | awk -F ',' '{print $2}')
	watchSMS=$(grep `date +%F` watchList.csv | awk -F ',' '{print $3}')
	watchMobi=$(grep `date +%F` watchList.csv | awk -F ',' '{print $4}')
	#先给自己测试
	if [[  $watchPeople == 'lty'  ]];then
	    #alerTime=`date +%s -d "$silenceTime min"`
	    #tigerTime=$(`cat  alterStatus.csv | tail -n 1 | awk -F '[,|.]' '{print $1}'` -d "$silenceTime min")
	    silenceTime=`cat  alterStatus.csv | tail -n 1  | awk -F  ',' '{print $2}'`
	    tigerTime=$( echo `cat  alterStatus.csv | tail -n 1 | awk -F '[,|.]' '{print $1}'` $silenceTime | awk '{print $1+$2}')
	    downTime=`date +%s -d '-10 min'`
	    morningTime=$(date +%s -d "`date +%F' '08:00:00`")
	    nightTime=$(date +%s -d "`date +%F' '22:00:00`")
	    #8为早上8点
	    if [[  $alerTime -eq 8 ]];then
	    	silenceTime=$morningTime
	    fi

	    #如果告警时间大于等于触发时间,就进行告警
	    if [[ $alerTime -gt $tigerTime   ]];then
	    	#其他告警,1.新增告警超过5条   2.mysql连接数2分钟超过90%  3.mysqldown 4.P2Pload 5.P2P在线为0  7.slb突增大于20% 7.kafka5分钟都大于3万堆积 8.磁盘小于3% 9.各种down
	    	#down告警,避免频繁告警10分钟未恢复的在告警
	    	downNum=`$mycnf -e "select activetime from collectAlarm where alertname like '%Down' and state=1 and activetime <= '$downTime';" | wc -l`
			alterNum=`cat $newAlertlog oldAlerts.log    | awk   -F '￥' '{print $NF}' |  sort | uniq -u | wc -l`
			alterType=`cat $newAlertlog  |  grep  -Ei "$majorAlarm"   |  wc -l`
			#告警数量是否大于5,告警类型是否大于1,down类型是否大于1
			if [[ $alterNum -gt 10  ]] || [[ $alterType -eq 1 ]]  || [[ $downNum -eq 1 ]]   ;then
				#白天钉钉告警,晚上增加一个短信告警,后续也可以是企业微信+短信结合
                if  [[ $alerTime -gt $morningTime   ]] ||  [[ $alerTime -lt $nightTime   ]] ;then
                	python3 /usr/local/shell/alter/libs/send.py  $watchPeople  	$watchMobi
                else
                	python3 /usr/local/shell/alter/libs/send.py  $watchPeople  	$watchMobi
                	#通过139邮箱达到短信告警目的,后续可替换阿里语音+短信告警
                	echo -e "九安告警:出现了影响业务告警,请及时查看"  | mutt -s "alter" $watchSMS
	        	fi
	        fi
	    else
	    	echo "静默时间段"
	    fi
	else
		echo "pass"
    fi
}

#是否用case并行跑
filterFunc
add_restore
update_value
sms_alter